﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WP7.InfoServiceRef;


namespace WP7
{
    public partial class MainPage : PhoneApplicationPage
    {
        // settings

        public static int second;
        public static String menu1Text;
        public static String MyIds;
        public static String type="";
        public static System.Device.Location.GeoCoordinateWatcher gcw;
        public static InfoServiceRef.InfoServiceClient sref;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            textBlock1.Visibility = System.Windows.Visibility.Collapsed;

            gcw = new System.Device.Location.GeoCoordinateWatcher();
            sref = new InfoServiceRef.InfoServiceClient();

            menu1Text = "Frissites";
            second = 10;
            sref.GetIDCompleted += new EventHandler<GetIDCompletedEventArgs>(MyId);
        }

        private static void MyId(object sender, InfoServiceRef.GetIDCompletedEventArgs id)
        {
            MainPage.MyIds = id.Result.First().ToString();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/pages/positionSend.xaml", UriKind.Relative));
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/pages/Search.xaml", UriKind.Relative));
        }

        private void menu1_Click(object sender, EventArgs e)
        {
            if (second < 60)
                second += 5;
            else
                second = 10;

            (ApplicationBar.MenuItems[0] as ApplicationBarMenuItem).Text = menu1Text + "  " + second + "s";
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            string error; 
            if (NavigationContext.QueryString.TryGetValue("error", out error)) 
            {
                button1.Visibility = System.Windows.Visibility.Collapsed;
                button2.Visibility = System.Windows.Visibility.Collapsed;
                button1.IsEnabled = false;
                button2.IsEnabled = false;
                if(error.Equals("dberror"))
                    textBlock1.Text = "Adatbázis hiba";


                textBlock1.Visibility = System.Windows.Visibility.Visible;
            }
        }

    }
}